<?php
/**
 * 框架 - 视图类
 * 
 * @version $Id: View.php 177 2011-05-18 07:46:43Z linsir123 $
 */
class View
{
	/**
	 * @var 模板引擎
	 */
	public $engine;
	
	/**
	 * @var 模板名称
	 */
	public $template;
	
	/**
	 * @var 模板缓存ID
	 */
	public $cacheId;
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		$app = App::instance();
		
		///
		if ($app->view['engine'] == 'Smarty') {
			App::import('View/smarty/Smarty.class.php');
			
			///
			$subApp = $app->subApp;
			empty($subApp) or $subApp = DS.$subApp;
			
			///
			lpDir::make(CACHE_DIR.'template/cache'.$subApp);
			lpDir::make(CACHE_DIR.'template/compile'.$subApp);
				
			///
			$engine = App::singleton('Smarty');
			$engine->left_delimiter = '<{';
			$engine->right_delimiter = '}>';
			$engine->caching = $app->view['caching'];
			$engine->cache_lifetime = $app->view['expired'];
			$engine->template_dir = APP_DIR.'Template'.$subApp;
			$engine->cache_dir = CACHE_DIR.'template/cache'.$subApp;
			$engine->compile_dir = CACHE_DIR.'template/compile'.$subApp;
			
			///
			$engine->assign('domain', App::getRequest()->domain);
			
			///
			$engine->register_modifier('__', array('I18n', '__'));
			$engine->register_modifier('substr', array('lpString', 'substr'));
			
			///
			$engine->register_function('url', array('lpUrl', 'smartyUrl'));
			
			///
			$this->engine = $engine;
		}
	}
	
	/**
	 * 由调度器来输出页面
	 */
	public function render()
	{
		if ($this->template) {
			if ($this->engine) 
				$this->engine->display($this->template, $this->cacheId);
			else {
				/// 没使用引擎
				
			}
		}
	}
	
	
	/// 以下方法提供为静态调用(将Smarty中常用的提出)  ///
	
	
	/**
	 * 将模板信息初始化
	 */
	static function display($template, $cacheId = null, $suffix = '.html')
	{
		$view = App::getView();
		
		///
		$view->template = $template.$suffix;
		$view->cacheId = $cacheId;
	}
	
	/**
	 * 判断模板是否缓
	 */
	static function isCached($template, $cacheId = null, $suffix = '.html')
	{
		$view = App::getView();
		
		///
		$view->template = $template.$suffix;
		$view->cacheId = $cacheId;
		if ($view->engine) 
			return $view->engine->is_cached($template.$suffix, $cacheId);
			
		return false;
	}
	
	/**
	 * Smarty的assign方法
	 */
	static function assign($name, $value)
	{
		$view = App::getView();
		
		///
		if ($view->engine) 
			$view->engine->assign($name, $value);
	}
}
